# 导包
import unittest

# 创建测试类
from IHRM import app
from IHRM.api.loginAPI import Login_API


class TestLogin(unittest.TestCase):

    # 前置处理
    def setUp(self) :
        self.login_api = Login_API()

    # 后置处理
    def tearDown(self) :
        pass

    # 定义测试方法
    # 登录成功
    def test_loginApi_001(self):

        # 请求登录接口中的URL
        # mobile = "13800000002"
        response = self.login_api.get_loginAPI({"mobile":"13800000002","password":"123456"})
        # 断言
        print(response.json())
        self.assertEqual(200, response.status_code)
        # "success":true,
        # "code":10000,
        # "message":"操作成功！",
        self.assertEqual(True,response.json().get("success"))
        self.assertEqual(10000,response.json().get("code"))
        self.assertIn("操作成功",response.json().get("message"))

        # 提取token信息
        app.TOKEN = "Bearer " + response.json().get("data")
        print(app.TOKEN)
        app.header_data["Authorization"] = app.TOKEN
        print("再次请求数据："+ app.header_data["Authorization"])

    # 不输入手机号
    def test_loginApi_002(self):
        response = self.login_api.get_loginAPI({"mobile": "", "password": "123456"})
        # 断言
        self.assertEqual(200, response.status_code)

        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))

        # 没有密码，少参
    def test_loginApi_003(self):
        response = self.login_api.get_loginAPI({"mobile": "13800000002"})
            # 断言
        self.assertEqual(200, response.status_code)

        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))